<?php
/**
 * Created by 胡祥能 on 2018/4/11.
 *  email：huxiangneng@jiuletech.com
 */

namespace App\Http\Controllers\Admin;

use App\Helpers\Api\ApiResponse;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Permission;
use Illuminate\Http\Request;
use Log;

class PermissionController extends DefaultController
{
    use ApiResponse;

    public function index(Request $request) {
        $perPage = $request->input('perPage')?:15;
//            $username = $request->input('username');
        $where = [];
//            if (!empty($username)) {
//                array_push($where, ['name', '=', $username]);
//            }

        $permissions = Permission::where($where)->paginate($perPage);
        return view('admin.system.permission', ['permissions'=>$permissions]);
    }

    public function add(Request $request)
    {
        if($request->method() == "POST"){
            $validator = Validator::make($request->all(), [
                'name' => 'bail|required|max:30',
                'display_name' => 'bail|required|max:30',
                'description' => 'max:50',
            ]);

            $name = $request->input('name');
            $res = Permission::where('name', $name)->first();
            if($res){
                $validator->errors()->add('name', '已存在该权限名');
                return back()->withInput()->withErrors($validator);
            }

            $permission = new Permission();
            $permission->name = $name;
            $permission->display_name = $request->input('display_name');
            $permission->description = $request->input('description');
            $permission->save();

            return $this->successView('', '/permissions');
        } else {
            return view('admin.system.permission-add');
        }
    }

    public function update(Request $request){
        if($request->method() == "GET"){
            $id = $request->input('id');
            if(empty($id)){
                return view('admin.404');
            }

            $permission = Permission::find($id);
            return view('admin.system.permission-update', ['permission'=>$permission]);
        }else{
            $validator = Validator::make($request->all(), [
                'name' => 'bail|required|max:30',
                'display_name' => 'bail|required|max:30',
                'description' => 'max:50',
            ]);
            $id = $request->input('id');
            $name = $request->input('name');
            $res = permission::where('name', $name)->where('id', '!=', $id)->first();
            if($res){
                $validator->errors()->add('name', '已存在该权限名');
                return back()->withInput()->withErrors($validator);
            }
            $permission = Permission::find($id);
            $permission->name = $name;
            $permission->display_name = $request->input('display_name');
            $permission->description = $request->input('description');
            $res = $permission->save();
            if($res){
                return $this->successView('', '/permissions');
            }else{
                return $this->failView('', '/permissions');
            }
        }
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if(empty($id)){
            return $this->json(1001);
        }

        $res = Permission::where('id', $id)->delete();

        if($res){
            //验证和用户、权限、菜单的关联是否与之删除
            DB::table('permission_role')->where('permission_id', $id)->delete();//删除权限和用户关联

            return $this->json(200);
        }else{
            return $this->json(1002);
        }
    }
}